package com.etc.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.etc.dao.UserDao;
import com.etc.entity.User;
import com.etc.service.UserService;
import com.etc.util.AjaxLogicBean;
import com.etc.util.MD5Util;
import com.etc.util.PageData;
@Service(value="userService")
public class UserServiceImpl implements UserService {
	@Resource(name="userDao")
	private UserDao userDao ;

	@Override
	public PageData<User> getAllUser(int page,int limit) {
		PageData<User> pd=new PageData<>();
		pd.setData(userDao.getUserAll(page, limit));
		pd.setLimit(limit);
		pd.setPage(page);
		pd.setCount(userDao.selectCount());
		return pd;
	}
	/**
	 * 通过账户密码获取用户
	 */
	@Override
	public User getUser(String userName, String userPwd) {
		String iuserPwd = MD5Util.getEncodeByMd5(userPwd);
		return userDao.getUser(userName, iuserPwd);
	}
	/**
	 * 分页+姓名模糊查询用户
	 */
	@Override
	public PageData<User> getUserByPage(int page, int pageSize, String content) {
		return userDao.getUserByPage(page, pageSize, content);
	}

	/**
	 * 保存用户
	 */
	@Override
	public boolean saveUser(User user) {
		// TODO Auto-generated method stub
		if (user != null) {
			user.setUserPwd(MD5Util.getEncodeByMd5(user.getUserPwd()));
			int n = userDao.saveUser(user);
			return n > 0;
		} else {
			return false;
		}

	}

	/**
	 * 通过id获取用户
	 */
	@Override
	public User getUserById(int userId) {
		return userDao.getUserById(userId);
	}

	/**
	 * 修改用户信息
	 */
	@Override
	public boolean updateUser(User user) {
		if (user != null) {
			int n = userDao.updateUser(user);
			return n > 0;
		} else {
			return false;
		}
	}

	/**
	 * 普通用户登录
	 */
	@Override
	public User getBUser(String userName, String userPwd) {
		String iuserPwd = MD5Util.getEncodeByMd5(userPwd);
		User user = userDao.getUser(userName, iuserPwd);
		if (user != null) {
			if (user.getUserState() == 1) {
				return user;
			} else {
				return null;
			}
		} else {
			return null;
		}

	}

	/**
	 * 根据id删除用户 返回true或false
	 */
	@Override
	public boolean delete(int userId) {
		int n = userDao.delete(userId);
		return n > 0;
	}
	/**
	 * 根据用户名查是否用户信息
	 */
	@Override
	public boolean finduser(String userName) {
		User user  = userDao.finduser(userName);
		if(user!=null) {
			return true;
		}
		return false;
	}

	@Override
	public AjaxLogicBean<User> getUserAll() {
		// TODO Auto-generated method stub
		List<User> list = userDao.getUserAll();
		AjaxLogicBean<User> pd = new AjaxLogicBean<>();
		pd.setData(list);
		return pd;
	}
}
